package com.cyx;


/**
 * @author 陈裕星
 * @version 1.0
 */
public class 前缀数组和 {
    static int[] preSum;
    public static void main(String[] args) {
        int[] nums = {3,5,2,-2,4,1};
        //构造前缀数组
        preSum = new int[nums.length + 1];
        for (int i = 1; i < preSum.length; i++) {
            preSum[i] = preSum[i-1] + nums[i-1];
        }
        int res = SumRange(2, 4);
        System.out.println(res);//4
    }

    //包括left和right
    static int SumRange(int left,int right){
        return preSum[right + 1] - preSum[left];
    }
}
